﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*
 * Author:Conis
 * Date:
 * Description:SQL语句中的Between语句单元
 */

namespace Conis.Squirrel.SqlBuilder
{
    /// <summary>
    /// SQL语句中的Between语句单元，形式如：fieldName BETWEEN 1 AND 1
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseBetweenUnit<T> : ConditionUnit
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string FieldName { get; set; }
        /// <summary>
        /// 开始的值
        /// </summary>
        public T StartValue { get; set; }
        /// <summary>
        /// 结束的值
        /// </summary>
        public T EndValue { get; set; }

        /// <summary>
        /// 循环前
        /// </summary>
        protected override void LoopBefore()
        {
            string startKey = "@" + Guid.NewGuid().ToString("N");
            string endKey = "@" + Guid.NewGuid().ToString("N");

            this.container.Append(this.FieldName).Append(" BETWEEN ")
                .Append(startKey).Append(" AND ").Append(endKey);
            this.paramsList.Add(this.CreateParameter(startKey, this.StartValue));
            this.paramsList.Add(this.CreateParameter(endKey, this.EndValue));
        }

        /// <summary>
        /// SQL语句中的Between，示例：fieldName Between And startValue And endValue
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="startValue">开始的值</param>
        /// <param name="endValue">结束的值</param>
        public BaseBetweenUnit(Connector connector, string fieldName, T startValue, T endValue)
        {
            this.Connector = connector;
            this.FieldName = fieldName;
            this.StartValue = startValue;
            this.EndValue = endValue;
        }
    }
}
